package com.lanyou.esb.cook.proxy.service.push.impl;

import java.text.SimpleDateFormat;

import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.entity.IfAccTimes;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.IfAccTimesJedisProxy;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.IfAccTimesKeysJedisProxy;
import com.lanyou.esb.cook.proxy.service.impl.IfAccTimesServiceImpl;
import com.lanyou.esb.cook.proxy.service.push.Push;

/**
 * 接口访问次数消息推送服务
 * 
 * @author Davey.wu
 */
public class IfAccTimesPushImpl extends BasePush<IfAccTimes> implements
		Push<IfAccTimes> {

	/** 接口平均响应时间Redis操作代理 **/
	private IfAccTimesJedisProxy jedisProxy;

	/** 接口访问次数键值Redis操作代理 **/
	private IfAccTimesKeysJedisProxy ifAccTimesKeysJedisProxy;

	private static final SimpleDateFormat DATEFORMATE_YYYYMMDD_NOSPACE = new SimpleDateFormat(
			Constants.DATE_PATTERN_YYYYMMDD_NOSPACE);

	@Override
	public void pushToRedis(IfAccTimes ifAccTimes) throws Exception {
		if (ifAccTimes == null) {
			return;
		}
		// 统计时间字符串
		String statDate = IfAccTimesServiceImpl.getDateStr(
				ifAccTimes.getStatDate(), DATEFORMATE_YYYYMMDD_NOSPACE);
		// 键
		String key = jedisProxy.getKeyFromFields(ifAccTimes.getFieldId(),
				ifAccTimes.getSystemId(), ifAccTimes.getIfId(), statDate);
		// 次数增加
		jedisProxy.setex(key, 1);
		// 加入接口名称
		key += IfAccTimesJedisProxy.KEY_SEPARATOR + ifAccTimes.getIfName();
		ifAccTimesKeysJedisProxy.saddAsStr(
				ifAccTimesKeysJedisProxy.getKey(), key);
	}

	public IfAccTimesJedisProxy getJedisProxy() {
		return jedisProxy;
	}

	public void setJedisProxy(IfAccTimesJedisProxy jedisProxy) {
		this.jedisProxy = jedisProxy;
	}

	public IfAccTimesKeysJedisProxy getIfAccTimesKeysJedisProxy() {
		return ifAccTimesKeysJedisProxy;
	}

	public void setIfAccTimesKeysJedisProxy(
			IfAccTimesKeysJedisProxy ifAccTimesKeysJedisProxy) {
		this.ifAccTimesKeysJedisProxy = ifAccTimesKeysJedisProxy;
	}
	
}
